package service.majorunit;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import util.manager.MajorunitManager;
import util.manager.PageManager;
import util.paramvo.MemberPageListParamVO;
import util.paramvo.SearchParamVO;
import util.returnvo.IndexReturnVO;
import util.returnvo.InterestListReturnVO;
import util.returnvo.MajorunitReturnVO;
import util.returnvo.RankingReturnVO;
import util.returnvo.SearchListReturnVO;
import vo.MajorunitVO;
import vo.MemberVO;
import vo.ReviewVO;
import vo.UnivVO;
import config.EstimateConfig;
import config.PageConfig;
import dao.examinfo.ExaminfoDAO;
import dao.interest.InterestDAO;
import dao.majorunit.MajorunitDAO;
import dao.region.RegionDAO;
import dao.review.ReviewDAO;
import dao.subline.SublineDAO;
import dao.univ.UnivDAO;

public class MajorunitServiceImpl implements MajorunitService {

	private MajorunitDAO majorunitDAO;
	private ReviewDAO reviewDAO;
	private InterestDAO interestDAO;
	private RegionDAO regionDAO;
	private SublineDAO sublineDAO;
	private ExaminfoDAO examinfoDAO;
	private UnivDAO univDAO;

	public void setExaminfoDAO(ExaminfoDAO examinfoDAO) {
		this.examinfoDAO = examinfoDAO;
	}

	public void setUnivDAO(UnivDAO univDAO) {
		this.univDAO = univDAO;
	}

	public void setMajorunitDAO(MajorunitDAO majorunitDAO) {
		this.majorunitDAO = majorunitDAO;
	}

	public void setReviewDAO(ReviewDAO reviewDAO) {
		this.reviewDAO = reviewDAO;
	}

	public void setInterestDAO(InterestDAO interestDAO) {
		this.interestDAO = interestDAO;
	}

	public void setRegionDAO(RegionDAO regionDAO) {
		this.regionDAO = regionDAO;
	}

	public void setSublineDAO(SublineDAO sublineDAO) {
		this.sublineDAO = sublineDAO;
	}

	@Override
	public MajorunitReturnVO getMajorunitReturnVO(MajorunitVO param) throws Exception {
		
		if(param==null) return null;
		
		int majorunit_no = param.getMajorunit_no();
		
		MajorunitReturnVO mrvo = reviewDAO.getAverageScore(majorunit_no);
		
		mrvo.setMajorunit_no(majorunit_no);
	
		String majorunit_name = MajorunitManager.convertUnitToString(param);
		mrvo.setMajorunit_name(majorunit_name);
	
		String majorunit_logo = param.getUniv().getUniv_logo();
		mrvo.setMajorunit_logo(majorunit_logo);
	
		int reviewCount = reviewDAO.getReviewCount(majorunit_no);
		mrvo.setReviewCount(reviewCount);
		
		UnivVO univ=univDAO.getUnivInfo(majorunit_no);
		mrvo.setUniv(univ);
		
		float levelAvg=examinfoDAO.getAverageLevel(majorunit_no);
		mrvo.setLevelAvg(levelAvg);
		
		return mrvo;
	}

	@Override
	public List<MajorunitReturnVO> getMajorunitReturnVOList(List<MajorunitVO> param) throws Exception {
		List<MajorunitReturnVO> returnList = new ArrayList<MajorunitReturnVO>();
		
		for(MajorunitVO vo : param){
			returnList.add(getMajorunitReturnVO(vo));
		}
		
		return returnList;
	}

	@Override
	public List<MajorunitReturnVO> getSearchedUnitByWord(List<String> wordList,
			String column, int univtype_no, int rowPerPage, int page)
			throws Exception {
		
		SearchParamVO spvo = new SearchParamVO(wordList, column, univtype_no,
				rowPerPage, page);

		List<MajorunitVO> mList = majorunitDAO.getSearchedUnitByWord(spvo);
		
		return getMajorunitReturnVOList(mList);
	}

	@Override
	public SearchListReturnVO getSearchedCompUnitByWord(String search,
			String column, String univtype_no, String page) throws Exception {
		int univNo = 0;
		if (univtype_no != null && !univtype_no.equals(""))
			univNo = Integer.parseInt(univtype_no);

		int pageNo = 1;
		if (page != null && !page.equals(""))
			pageNo = Integer.parseInt(page);
		
		if(column==null || column.equals("")) column=EstimateConfig.columns[0];

		int rowPerPage = PageConfig.SEARCH_ROW_PER_PAGE;
		
		if(search==null) search="";
		
		StringTokenizer st = new StringTokenizer(search);
		
		ArrayList<String> wordList = new ArrayList<String>();
		
		while (st.hasMoreElements()) {
			wordList.add(st.nextToken());
		}
		
		if(wordList.isEmpty()) wordList.add("");

		List<MajorunitReturnVO> majorunitList = getSearchedUnitByWord(wordList,
				column, univNo, rowPerPage, pageNo);

		int pagePerGroup = PageConfig.SEARCH_PAGE_PER_GROUP;

		SearchParamVO svo = new SearchParamVO(wordList, univNo);

		int totalRow = majorunitDAO.countSearchedUnitByWord(svo);

		PageManager pageManager = new PageManager(rowPerPage, pagePerGroup,
				totalRow, pageNo);

		return new SearchListReturnVO(majorunitList, pageManager);
	}

	@Override
	public List<MajorunitReturnVO> getSearchedUnitBySubline(int subline_no,
			String column, int univtype_no, int rowPerPage, int page)
			throws Exception {
		SearchParamVO svo = new SearchParamVO(column, subline_no, univtype_no,
				rowPerPage, page);

		List<MajorunitVO> mList = majorunitDAO.getSearchedUnitBySubline(svo);

		return getMajorunitReturnVOList(mList);
	}

	@Override
	public SearchListReturnVO getSearchedCompUnitBySubline(int subline_no,
			String column, String univtype_no, String page) throws Exception {
		int univNo = 0;
		if (univtype_no != null && !univtype_no.equals(""))
			univNo = Integer.parseInt(univtype_no);

		int pageNo = 1;
		if (page != null && !page.equals(""))
			pageNo = Integer.parseInt(page);
		
		if(column==null || column.equals("")) column=EstimateConfig.columns[0];

		int rowPerPage = PageConfig.SEARCH_ROW_PER_PAGE;

		List<MajorunitReturnVO> majorunitList = getSearchedUnitBySubline(
				subline_no, column, univNo, rowPerPage, pageNo);

		int pagePerGroup = PageConfig.SEARCH_PAGE_PER_GROUP;

		SearchParamVO svo = new SearchParamVO(column, subline_no, univNo,
				rowPerPage, pageNo);

		int totalRow = majorunitDAO.countSearchedUnitBySubline(svo);

		PageManager pageManager = new PageManager(rowPerPage, pagePerGroup,
				totalRow, pageNo);

		return new SearchListReturnVO(majorunitList, pageManager);
	}

	@Override
	public List<MajorunitReturnVO> getSearchedUnitByRegion(int region_no,
			String column, int univtype_no, int rowPerPage, int page)
			throws Exception {
		SearchParamVO svo = new SearchParamVO(region_no, column, univtype_no,
				rowPerPage, page);

		List<MajorunitVO> mList = majorunitDAO.getSearchedUnitByRegion(svo);

		return getMajorunitReturnVOList(mList);
	}

	@Override
	public SearchListReturnVO getSearchedCompUnitByRegion(int region_no,
			String column, String univtype_no, String page) throws Exception {
		int univNo = 0;
		if (univtype_no != null && !univtype_no.equals(""))
			univNo = Integer.parseInt(univtype_no);

		int pageNo = 1;
		if (page != null && !page.equals(""))
			pageNo = Integer.parseInt(page);
		
		if(column==null || column.equals("")) column=EstimateConfig.columns[0];

		int rowPerPage = PageConfig.SEARCH_ROW_PER_PAGE;

		List<MajorunitReturnVO> majorunitList = getSearchedUnitByRegion(
				region_no, column, univNo, rowPerPage, pageNo);

		int pagePerGroup = PageConfig.SEARCH_PAGE_PER_GROUP;

		SearchParamVO svo = new SearchParamVO(region_no, univNo);

		int totalRow = majorunitDAO.countSearchedUnitByRegion(svo);

		PageManager pageManager = new PageManager(rowPerPage, pagePerGroup,
				totalRow, pageNo);

		return new SearchListReturnVO(majorunitList, pageManager);
	}

	@Override
	public MajorunitReturnVO getEstimateTopUnit(String column) throws Exception {
		MajorunitVO mvo = majorunitDAO.getTopUnit(column);

		MajorunitReturnVO mrvo = getMajorunitReturnVO(mvo);

		return mrvo;
	}

	@Override
	public MajorunitReturnVO getRegionTopUnit(int region_no) throws Exception {
		MajorunitVO mvo = majorunitDAO.getRegionTopUnit(region_no);

		MajorunitReturnVO mrvo = getMajorunitReturnVO(mvo);

		return mrvo;
	}

	@Override
	public IndexReturnVO getIndexReturnVO(String member_no) throws Exception {
		IndexReturnVO ivo = new IndexReturnVO();

		MajorunitVO scoreTopMvo = majorunitDAO
				.getTopUnit(EstimateConfig.columns[0]);
		MajorunitVO jobTopMvo = majorunitDAO
				.getTopUnit(EstimateConfig.columns[1]);
		MajorunitVO professorTopMvo = majorunitDAO
				.getTopUnit(EstimateConfig.columns[2]);
		MajorunitVO facilityTopMvo = majorunitDAO
				.getTopUnit(EstimateConfig.columns[3]);
		MajorunitVO scholarshipTopMvo = majorunitDAO
				.getTopUnit(EstimateConfig.columns[4]);
		MajorunitVO awarenessTopMvo = majorunitDAO
				.getTopUnit(EstimateConfig.columns[5]);

		int regionCount = regionDAO.getTotalCount();
		int ranRegion = (int) (Math.random() * regionCount + 1);
		MajorunitVO regionTopMvo = majorunitDAO.getRegionTopUnit(ranRegion);
		
		int lineCount = sublineDAO.getTotalCount();
		int ranLine = (int) (Math.random() * lineCount + 1);
		MajorunitVO lineTopMvo = majorunitDAO.getLineTopUnit(ranLine);

		ivo.setScoreTop(getMajorunitReturnVO(scoreTopMvo));
		ivo.setJobTop(getMajorunitReturnVO(jobTopMvo));
		ivo.setProfessorTop(getMajorunitReturnVO(professorTopMvo));
		ivo.setFacilityTop(getMajorunitReturnVO(facilityTopMvo));
		ivo.setScholarshipTop(getMajorunitReturnVO(scholarshipTopMvo));
		ivo.setAwarenessTop(getMajorunitReturnVO(awarenessTopMvo));
		ivo.setRegionTop(getMajorunitReturnVO(regionTopMvo));
		ivo.setLineTop(getMajorunitReturnVO(lineTopMvo));

		if (member_no != null && !member_no.equals("")) {
			int memNo = Integer.parseInt(member_no);

			if (interestDAO.countInterestedUnit(memNo) != 0) {
				ReviewVO interestNew = reviewDAO.getNewestInterestUnitReview(memNo);
				ivo.setInterestNew(interestNew);
			}
		}

		return ivo;
	}

	@Override
	public MajorunitReturnVO getOneUnitReturn(int majorunit_no)
			throws Exception {
		MajorunitVO mvo = majorunitDAO.getOneUnit(majorunit_no);

		MajorunitReturnVO mrvo = getMajorunitReturnVO(mvo);

		return mrvo;
	}

	@Override
	public List<String> getAutoCompleteList(String search) throws Exception {

		ArrayList<String> wordList = new ArrayList<String>();
		
		if(search==null || search.equals("")) return wordList;
		
		StringTokenizer st = new StringTokenizer(search);// default가 " "임

		while (st.hasMoreElements()) {
			wordList.add(st.nextToken());
		}

		String column = EstimateConfig.columns[0];
		int rowPerPage = PageConfig.AUTOCOMPL_ROW_PER_PAGE;

		SearchParamVO svo = new SearchParamVO(wordList, column, 0, rowPerPage,
				1);

		List<MajorunitVO> mList = majorunitDAO.getSearchedUnitByWord(svo);

		List<String> sList = new ArrayList<String>();

		for (MajorunitVO mvo : mList) {
			String mName = MajorunitManager.convertUnitToString(mvo);
			sList.add(mName);
		}

		return sList;
	}

	@Override
	public MajorunitReturnVO getOneUnitForReview(int member_no, int majorunit_no)
			throws Exception {
		MajorunitReturnVO mvo = null;

		ReviewVO rvo = new ReviewVO(new MemberVO(member_no), new MajorunitVO(
				majorunit_no));

		if (reviewDAO.didWriteReview(rvo) == 1) {
			mvo = new MajorunitReturnVO(-1);
		} else {
			mvo = getMajorunitReturnVO(majorunitDAO.getOneUnit(majorunit_no));
		}

		return mvo;
	}

	@Override
	public InterestListReturnVO getInterestedUnit(int member_no, String page)
			throws Exception {
		int pageNo = 1;
		if (page != null && !page.equals(""))
			pageNo = Integer.parseInt(page);

		MemberPageListParamVO ivo = new MemberPageListParamVO(member_no,
				PageConfig.INTEREST_ROW_PER_PAGE, pageNo);

		List<MajorunitVO> mList = majorunitDAO.getInterestedUnit(ivo);

		List<MajorunitReturnVO> reList = getMajorunitReturnVOList(mList);

		int rowPerPage = PageConfig.INTEREST_ROW_PER_PAGE;

		int pagePerGroup = PageConfig.INTEREST_PAGE_PER_GROUP;

		int totalRow = interestDAO.countInterestedUnit(member_no);

		PageManager pageManager = new PageManager(rowPerPage, pagePerGroup,
				totalRow, pageNo);

		return new InterestListReturnVO(reList, pageManager);
	}

	@Override
	public List<MajorunitReturnVO> getSearchedUnitByLine(int line_no,
			String column, int univtype_no, int rowPerPage, int page)
			throws Exception {
		SearchParamVO svo = new SearchParamVO(line_no, univtype_no, rowPerPage, page, column);

		List<MajorunitVO> mList = majorunitDAO.getSearchedUnitByLine(svo);

		return getMajorunitReturnVOList(mList);
	}

	@Override
	public SearchListReturnVO getSearchedCompUnitByLine(int line_no,
			String column, String univtype_no, String page) throws Exception {
		int univNo = 0;
		if (univtype_no != null && !univtype_no.equals(""))
			univNo = Integer.parseInt(univtype_no);

		int pageNo = 1;
		if (page != null && !page.equals(""))
			pageNo = Integer.parseInt(page);
		
		if(column==null || column.equals("")) column=EstimateConfig.columns[0];

		int rowPerPage = PageConfig.SEARCH_ROW_PER_PAGE;

		List<MajorunitReturnVO> majorunitList = getSearchedUnitByLine(line_no, column, univNo, rowPerPage, pageNo);

		int pagePerGroup = PageConfig.SEARCH_PAGE_PER_GROUP;

		SearchParamVO svo = new SearchParamVO(line_no, univNo, rowPerPage, pageNo, column);

		int totalRow = majorunitDAO.countSearchedUnitByLine(svo);

		PageManager pageManager = new PageManager(rowPerPage, pagePerGroup,
				totalRow, pageNo);

		return new SearchListReturnVO(majorunitList, pageManager);
	}

	@Override
	public MajorunitReturnVO getLineTopUnit(int subline_no) throws Exception {
		MajorunitVO mvo = majorunitDAO.getLineTopUnit(subline_no);

		MajorunitReturnVO mrvo = getMajorunitReturnVO(mvo);

		return mrvo;
	}

	@Override
	public RankingReturnVO getRankingReturnVO() throws Exception {
		RankingReturnVO ranking=new RankingReturnVO();
		
		ranking.setScoreTop3(getMajorunitReturnVOList((majorunitDAO.getTop3Unit(EstimateConfig.columns[0]))));
		ranking.setJobTop3(getMajorunitReturnVOList((majorunitDAO.getTop3Unit(EstimateConfig.columns[1]))));
		ranking.setProfessorTop3(getMajorunitReturnVOList((majorunitDAO.getTop3Unit(EstimateConfig.columns[2]))));
		ranking.setFacilityTop3(getMajorunitReturnVOList((majorunitDAO.getTop3Unit(EstimateConfig.columns[3]))));
		ranking.setScholarshipTop3(getMajorunitReturnVOList((majorunitDAO.getTop3Unit(EstimateConfig.columns[4]))));
		ranking.setAwarenessTop3(getMajorunitReturnVOList((majorunitDAO.getTop3Unit(EstimateConfig.columns[5]))));
		
		return ranking;
	}
	
	@Override
	public SearchListReturnVO getPopupSearchedUnit(String search, String page) throws Exception {
		int pageNo = 1;
		if (page != null && !page.equals(""))
			pageNo = Integer.parseInt(page);
		
		int rowPerPage = PageConfig.POPUP_ROW_PER_PAGE;
		
		if(search==null) search="";
		
		StringTokenizer st = new StringTokenizer(search);
		
		ArrayList<String> wordList = new ArrayList<String>();
		
		while (st.hasMoreElements()) {
			wordList.add(st.nextToken());
		}
		
		if(wordList.isEmpty()) wordList.add("");

		List<MajorunitReturnVO> majorunitList = getSearchedUnitByWord(wordList,
				EstimateConfig.columns[0], 0, rowPerPage, pageNo);

		int pagePerGroup = PageConfig.POPUP_PAGE_PER_GROUP;

		SearchParamVO svo = new SearchParamVO(wordList, 0);

		int totalRow = majorunitDAO.countSearchedUnitByWord(svo);

		PageManager pageManager = new PageManager(rowPerPage, pagePerGroup,
				totalRow, pageNo);

		return new SearchListReturnVO(majorunitList, pageManager);
	}
}
